package com.spic.business.plan.drill.service.impl;

import com.spic.business.common.datascope.anotation.BusinessDataScope;
import com.spic.business.plan.book.service.IPlanStandingBookService;
import com.spic.business.plan.drill.domain.PlanDrillPerformInfo;
import com.spic.business.plan.drill.domain.PlanDrillPerformStep;
import com.spic.business.plan.drill.mapper.PlanDrillPerformInfoMapper;
import com.spic.business.plan.drill.service.IPlanDrillPerformInfoService;
import com.spic.business.plan.drill.service.IPlanDrillPerformStepService;
import com.spic.business.plan.exerciseDetail.domain.PlanExerciseDetail;
import com.spic.business.plan.exerciseDetail.service.IPlanExerciseDetailService;
import com.spic.business.two.tickets.general.utils.CharacterConversionUtils;
import com.spic.common.core.utils.DateUtils;
import com.spic.common.core.utils.StringUtils;
import com.spic.system.api.RemoteDeptService;
import com.spic.system.api.RemoteUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 应急预案演练与培训Service业务层处理
 *
 * @author renxiang
 * @date 2021-10-29
 */
@Service
public class PlanDrillPerformInfoServiceImpl implements IPlanDrillPerformInfoService
{
    @Autowired
    private PlanDrillPerformInfoMapper planDrillPerformInfoMapper;

    @Autowired
    private IPlanDrillPerformStepService planDrillPerformStepService;

    @Autowired
    private IPlanExerciseDetailService planExerciseDetailService;

    @Autowired
    private IPlanStandingBookService planStandingBookService;

    // 调用 计数器用
    @Autowired
    private RemoteUserService remoteUserService;

    @Autowired
    private RemoteDeptService deptService;



    /**
     * 查询应急预案演练与培训
     *
     * @param id 应急预案演练与培训主键
     * @return 应急预案演练与培训
     */
    @Override
    public PlanDrillPerformInfo selectPlanDrillPerformInfoById(Long id)
    {
        PlanDrillPerformInfo info = planDrillPerformInfoMapper.selectPlanDrillPerformInfoById(id);
        PlanDrillPerformStep step = new PlanDrillPerformStep();
        step.setDrillNumber(info.getDrillNumber());
        info.setPlanDrillPerformStepList(planDrillPerformStepService.selectPlanDrillPerformStepList(step));
        return info;
    }

    /**
     * 查询应急预案演练与培训列表
     *
     * @param planDrillPerformInfo 应急预案演练与培训
     * @return 应急预案演练与培训
     */
    @Override
    @BusinessDataScope(menuPath = "business/plan/drillPerform/index")
    public List<PlanDrillPerformInfo> selectPlanDrillPerformInfoList(PlanDrillPerformInfo planDrillPerformInfo)
    {
        return planDrillPerformInfoMapper.selectPlanDrillPerformInfoList(planDrillPerformInfo);
    }

    /**
     * 新增应急预案演练与培训
     *
     * @param planDrillPerformInfo 应急预案演练与培训
     * @return 结果
     */
    @Transactional
    @Override
    public PlanDrillPerformInfo insertPlanDrillPerformInfo(PlanDrillPerformInfo planDrillPerformInfo)
    {
        String organizeDepartment  = planDrillPerformInfo.getOrganizeDepartment();
        String planType = "";
        String deptCode = "";
        PlanExerciseDetail detail = planExerciseDetailService.selectPlanExerciseDetailById(planDrillPerformInfo.getPlanName());
        if (null != detail){
            planType = planStandingBookService.selectPlanStandingBookById(Long.valueOf(detail.getStandingBookId())).getClassify();
        }
        if (StringUtils.isNotNull(organizeDepartment) && null != Long.valueOf(organizeDepartment)){
            //获取部门大写首字母
            deptCode = CharacterConversionUtils.getUpperHeadChar(deptService.getDeptName(Long.valueOf(organizeDepartment)).getData());
        }
        String code  = remoteUserService.getCode("预案演练编号").getData();
        // 预案演练编号 添加计数
        planDrillPerformInfo.setDrillNumber(planType  + "-" + deptCode + "-" + code);
        planDrillPerformInfo.setCreateTime(DateUtils.getNowDate());
        int rows = planDrillPerformInfoMapper.insertPlanDrillPerformInfo(planDrillPerformInfo);
        return planDrillPerformInfo;
    }

    /**
     * 修改应急预案演练与培训
     *
     * @param planDrillPerformInfo 应急预案演练与培训
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePlanDrillPerformInfo(PlanDrillPerformInfo planDrillPerformInfo)
    {
        planDrillPerformInfo.setUpdateTime(DateUtils.getNowDate());
        int rows = planDrillPerformInfoMapper.updatePlanDrillPerformInfo(planDrillPerformInfo);
        return rows;
    }

    /**
     * 批量删除应急预案演练与培训
     *
     * @param ids 需要删除的应急预案演练与培训主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePlanDrillPerformInfoByIds(Long[] ids)
    {
        return planDrillPerformInfoMapper.deletePlanDrillPerformInfoByIds(ids);
    }

    /**
     * 删除应急预案演练与培训信息
     *
     * @param id 应急预案演练与培训主键
     * @return 结果
     */
    @Override
    public int deletePlanDrillPerformInfoById(Long id)
    {
        return planDrillPerformInfoMapper.deletePlanDrillPerformInfoById(id);
    }

}
